package Dao;

import models.DailyDispage;
import models.IUser;
import models.User;
import models.daily;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 郑凯 on 2016/6/17.
 */
public class UserOp {

    public static  User getUserByUid(String uid){
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        User u = iUser.GetUserByUid(uid);
        sqlSession.commit();
        sqlSession.close();
        return u;
    }
    public static List getDailyByUid(String uid){
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        List<daily> listdaily = iUser.GetDailyByUid(uid);
        sqlSession.commit();
        sqlSession.close();
        return  listdaily;
    }
    public static List GetDailyDispage(DailyDispage dispage)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        List<daily> listdaily = iUser.GetDailyDispage(dispage);
        sqlSession.commit();
        sqlSession.close();
        return  listdaily;
    }
    public static daily getDailyByDid(String did)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        daily d1 = new daily();
        d1.setDid(did);

        daily d = iUser.GetDailyByDid(d1);
        sqlSession.commit();
        sqlSession.close();
        return d;
    }
    public static boolean saveDaily(String title, String content, String tab, String uid)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        daily d = new daily();
        d.setDid(String.valueOf(new Date().getTime()));
        d.setTitle(title);
        d.setContent(content);
        d.setTime( dateFormat.format(new Date()));
        d.setUid(uid);
        d.setTab(tab);


        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        iUser.insertDaily(d);
        sqlSession.commit();
        updateUserDaily(uid);
        sqlSession.close();
        return true;
    }
    public static void updateUserDaily(String uid)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        int count = iUser.SearchUserDaily(uid);
        User user = new User();
        user.setUid(uid);
        user.setDaily(count);
        iUser.UpdateUserDaily(user);
        sqlSession.commit();
        sqlSession.close();
    }
    public static int getCountDailyDispageByUid(String uid)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        int sum = iUser.SearchUserDaily(uid);
        sqlSession.commit();
        sqlSession.close();
        return sum;
    }
    public static boolean deleteDailyByDid(String did)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        daily d = new daily();
        d.setDid(did);
        iUser.DeleteDailyByDid(d);
        sqlSession.commit();
        sqlSession.close();
        return true;
    }
    public static void  updateDailyByDid(daily d)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        iUser.UpdateDailyByDid(d);
        sqlSession.commit();
        sqlSession.close();
    }
    public static void updateUserByUid(User user)
    {
        SqlSession sqlSession = mybatis_GetSessionFactory.getSqlSessionFactory().openSession();
        IUser iUser = sqlSession.getMapper(IUser.class);
        iUser.UpdateUserByuid(user);
        sqlSession.commit();
        sqlSession.close();
    }
}
